package cn.tedu.user.management.controller;

import cn.tedu.rtms.commons.pojo.vo.PageData;
import cn.tedu.rtms.commons.web.JsonResult;
import cn.tedu.user.management.pojo.param.SectionAddNewParam;
import cn.tedu.user.management.pojo.param.SectionUpdateInfoParam;
import cn.tedu.user.management.pojo.vo.SectionListItemVO;
import cn.tedu.user.management.pojo.vo.SectionStandardVO;
import cn.tedu.user.management.pojo.vo.SectionTreeItemVO;
import cn.tedu.user.management.service.ISectionService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 处理部门相关请求的控制器
 *
 * @author java@tedu.cn
 * @version 0.0.1
 */
@Slf4j
@RestController
@RequestMapping("/sections")
@Validated
@Api(tags = "02. 部门管理模块")
public class SectionController {

    @Autowired
    private ISectionService sectionService;

    public SectionController() {
        log.debug("创建控制器类对象：SectionController");
    }

    // http://localhost:9250/sections/add-new
    @PostMapping("/add-new")
    @PreAuthorize("hasAuthority('/sections/add-new')")
    @ApiOperation("新增部门")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> reg(@Valid SectionAddNewParam sectionAddNewParam) {
        log.debug("开始处理【新增部门】的请求，参数：{}", sectionAddNewParam);
        sectionService.addNew(sectionAddNewParam);
        return JsonResult.ok();
    }

    // http://localhost:9250/sections/9527/delete
    @PostMapping("/{id:[0-9]+}/delete")
    @PreAuthorize("hasAuthority('/sections/delete')")
    @ApiOperation("根据ID删除部门")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "部门ID", required = true, dataType = "long")
    })
    public JsonResult<Void> delete(@PathVariable Long id) {
        log.debug("开始处理【根据ID删除部门】的请求，参数：{}", id);
        sectionService.delete(id);
        return JsonResult.ok();
    }


    // http://localhost:9250/sections
    @GetMapping("")
    @ApiOperation("查询部门列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", dataType = "long")
    })
    public JsonResult<PageData<SectionListItemVO>> list(Integer page) {
        log.debug("开始处理【查询部门列表】的请求，页码：{}", page);
        Integer pageNum = page == null ? 1 : page;
        PageData<SectionListItemVO> pageData = sectionService.list(pageNum);
        return JsonResult.ok(pageData);
    }

    // http://localhost:9250/sections
    @GetMapping("/sectionNameList")
    @ApiOperation("查询部门列表(不分页)")
    @ApiOperationSupport(order = 440)
    public JsonResult<List<SectionListItemVO>> sectionNameList() {
        log.debug("开始处理【查询部门名称列表(不分页)】的请求");
        List<SectionListItemVO> list = sectionService.list();
        return JsonResult.ok(list);
    }

    // http://localhost:9250/childrenList
    @GetMapping("/childrenList")
    @ApiOperation("查询子级部门列表(不分页)")
    @ApiOperationSupport(order = 450)
    public JsonResult<List<SectionListItemVO>> childrenList(Long parentId) {
        log.debug("开始处理【查询子级部门列表(不分页)】的请求:{}",parentId);
        List<SectionListItemVO> list = sectionService.list(parentId);
        return JsonResult.ok(list);
    }

    // http://localhost:9250/sections/9527/update
    @PostMapping("/{id:[0-9]+}/update")
    @PreAuthorize("hasAuthority('/sections/update')")
    @ApiOperation("修改部门信息")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "部门ID", required = true, dataType = "long")
    })
    public JsonResult<Void> updateInfoById(@PathVariable @Range(min = 1, message = "请提交有效的部门ID值！") Long id,
                                           @Valid SectionUpdateInfoParam sectionUpdateInfoParam) {
        log.debug("开始处理【修改部门信息】的请求，ID：{}，新数据：{}", id, sectionUpdateInfoParam);
        sectionService.updateInfoById(id, sectionUpdateInfoParam);
        return JsonResult.ok();
    }

    // http://localhost:9250/sections/9527
    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查询部门信息")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "部门ID", required = true, dataType = "long")
    })
    public JsonResult<SectionStandardVO> getStandardById(
            @PathVariable @Range(min = 1, message = "请提交有效的部门ID值！") Long id) {
        log.debug("开始处理【根据ID查询部门信息】的请求，参数：{}", id);
        SectionStandardVO queryResult = sectionService.getStandardById(id);
        return JsonResult.ok(queryResult);
    }

    // http://localhost:9250/sections/tree
    @GetMapping("/tree")
    @ApiOperation("查询部门树")
    @ApiOperationSupport(order = 420)
    public JsonResult<List<SectionTreeItemVO>> listTree() {
        log.debug("开始处理【获取部门树】的业务，参数：无");
        List<SectionTreeItemVO> sectionTree = sectionService.listTree();
        return JsonResult.ok(sectionTree);
    }

    // http://localhost:9250/sections/enableTree
    @GetMapping("/enableTree")
    @ApiOperation("查询招聘部门树")
    @ApiOperationSupport(order = 430)
    public JsonResult<List<SectionTreeItemVO>> listEnableTree() {
        log.debug("开始处理【获取招聘部门树】的业务，参数：无");
        List<SectionTreeItemVO> sectionTree = sectionService.listEnableTree();
        return JsonResult.ok(sectionTree);
    }

}